--[[
    desc: 魂石突破结果界面
    author:Hoo
    time:2022-01-06 14:17:29
]]

--- @class UIDebrisBaptismResult
local UIDebrisBaptismResult = UIBase.New(PanelResNames.UIDebrisBaptismResult)
local M = UIDebrisBaptismResult

--- 魂石id
--- @type number
local mItemId

-- 属性列表
--- @type any[]
local mAttrList = {}

-- 突破技能列表
--- @type DebrisSubSkillProtoBean[]
local mSkillList = {}

function M.Init(itemId, attrData, skillData)
    mItemId    = itemId
    mAttrList  = attrData
    mSkillList = skillData

    UIBase.Init(M)
end

--- @protected
function M:OnCreate()
    -- 展示区魂石
    self.debrisItem = self:GetChild("item")

    -- 属性列表
    self.listAttr = self:GetChild("attrList")
    self:SetDynamicList(self.listAttr, self.attrItemRenderer, self, true)
    -- 突破技能列表
    self.listSkill = self:GetChild("skillList")
    self:SetDynamicList(self.listSkill, self.skillItemRenderer, self, true)

    -- 特效
    self.effParent = self:GetChild("eff_starUp")

    self:GetChild("title").text        = getLanguage("DebrisText14")
    self:GetChild("BaptismTips1").text = getLanguage("DebrisText15")
    self:GetChild("BaptismTips2").text = getLanguage("DebrisText16")
    self:RegisterOnClick(self:GetChild("bgClose"), self.onClickClose, self)
end
--- @protected
function M:OnEnable()
    self:initUI()
end
--- @protected
function M:OnDisable()
end
--- @protected
function M:OnDestroy()
    mAttrList       = nil
    mSkillList      = nil

    self.effWrapper = nil
    if self.effBroken ~= nil then
        destroyImmediate(self.effBroken);
        self.effBroken = nil
    end
end

--- 初始化UI
function M:initUI()
    local itemData = StrangeFragmentManager.GetDebrisByItemid(mItemId)
    local item     = CtrlManager.GetCtrl(PanelNames.Item)
    if itemData ~= nil and item ~= nil then
        StrangeFragmentManager.SetItemData(self.debrisItem, itemData, false)
        self.debrisItem.onClick:Clear()
    end

    self.listAttr.numItems  = #mAttrList
    self.listSkill.numItems = #mSkillList

    self:playAni()
end

--- 属性列表item
--- @private
--- @param index number  索引
--- @param obj   GObject 属性item对象
function M:attrItemRenderer(index, obj)
    local txtName      = obj:GetChild("title")
    local txtCurrValue = obj:GetChild("preNum")
    local txtNextValue = obj:GetChild("num")

    local ctrlAttrType = obj:GetController("type")
    ctrlAttrType.selectedIndex = index == 1 and 3 or 0

    local attr = mAttrList[index]
    local cfg  = dataTable.getDataTableByStringId("t_attributelibrary", tostring(attr[1]))
    if cfg then
        txtName.text = cfg.f_AttributeName
    end
    if index == 1 then
        txtName.text = getLanguage("DebrisText20")
    end
    txtCurrValue.text = tostring(attr[2])
    txtNextValue.text = tostring(attr[3])
end

--- 突破技能列表item
--- @private
--- @param index number  索引
--- @param obj   GObject 技能item对象
function M:skillItemRenderer(index, obj)
    local txtDesc = obj:GetChild("n4"):GetChild("textSkillType")
    local ctrlHighLevel = obj:GetController("ifHighLevel")

    local skillData = mSkillList[index]
    if skillData == nil then
        txtDesc.text = ""
        return
    end

    local id, value = skillData.skillId, skillData.val
    local cfg = dataTable.getDataTableByStringId("t_debrisminorskill", tostring(id))
    txtDesc.text = cfg and string.format(getLanguage(cfg.f_text), value * cfg.f_muti * 0.0001) or ""
    ctrlHighLevel.selectedIndex = self:isHighLevel(id, value) and 1 or 0
end

--- 检测技能是否未高级技能
--- @private
--- @param id    number 技能id
--- @param value number 技能数值
--- @return boolean
function M:isHighLevel(id, value)
    local cfg = dataTable.getDataTableByStringId("t_debrisminorskill", tostring(id))
    if cfg and math.abs(value) >= math.abs(cfg.f_RareValue) then
        return true
    end
    return false
end

--- 突破后动画
---@private
function M:playAni()
    resMgr:LoadModelPrefab("UIEff/UI_starUp_01", function(prefab)
        if self.effWrapper == nil then
            self.effWrapper = GoWrapper.New();
        end
        if self.effBroken ~= nil then
            destroyImmediate(self.effBroken);
            self.effBroken = nil
        end
        self.effBroken = newObject(prefab)
        self.effWrapper:SetWrapTarget(self.effBroken,false)
        Util.Lua_SetLocalRotationXYZ(self.effBroken.transform, 0, 0, 0)
        self.effParent:SetNativeObject(self.effWrapper)
    end)
end

--- 关闭界面
--- @private
function M:onClickClose()
    closeUI(PanelResNames.UIDebrisBaptismResult)
end

return M
